package com.zbdx.dao.impl;

import com.zbdx.dao.SelProductDao;
import com.zbdx.domain.product;
import com.zbdx.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @version 1.0
 * @Auther 樊航宇
 * @Date 2024/9/15 17:14
 * @注释
 */
public class SelProductDaoImpl implements SelProductDao {


   private QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

   /**
    * 按照type查询数据库里面的商品
    * @param PType
    * @return
    */
   @Override
   public List<product> selAllProduct(String PType) throws SQLException {

      String sql = "select * from product where PType = ?";

      List<product> product = qr.query(sql,new BeanListHandler<product>(product.class),PType);

      return product;
   }

   /**
    * 根据Type查询销量前10的各个类型的商品
    *
    * @param ptype
    */
   @Override
   public List<product> selHeadProduct(String ptype) throws SQLException {

      String sql = "select * from product where PType = ? ORDER BY PSalesVolume DESC LIMIT 10;";

      List<product> product = qr.query(sql,new BeanListHandler<product>(product.class),ptype);

      return product;
   }

}
